﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SqlSugarTest
{
    public class DbContext<T> where T : class, new()
    {
        public DbContext()
        {
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "server=10.61.140.15;uid=sa;pwd=shris@2020;database=ChangDeScada",
                DbType = DbType.SqlServer,
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
            });

            //调试用代码
            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                string s = sql;
                Console.WriteLine(sql + "\r\n" +
                    Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
        }


        public SqlSugarClient Db;

        public SimpleClient<T> CurrentDb
        {
            get
            {
                return new SimpleClient<T>(Db);
            }
        }

        //public SimpleClient<WorkShops> WorkShopsDb
        //{
        //    get
        //    {
        //        return new SimpleClient<WorkShops>(Db);
        //    }
        //}

        //public SimpleClient<Machines> MachinesDb
        //{
        //    get
        //    {
        //        return new SimpleClient<Machines>(Db);
        //    }
        //}

        //public SimpleClient<DownTimes> DownTimesDb
        //{
        //    get
        //    {
        //        return new SimpleClient<DownTimes>(Db);
        //    }
        //}


        public virtual List<T> GitList()
        {
            return CurrentDb.GetList();
        }

        public virtual bool Delete(dynamic id)
        {
            return CurrentDb.Delete(id);
        }

        public virtual bool Update(T obj)
        {
            return CurrentDb.Update(obj);
        }

    }
}
